package com.zlll.winner.service.media;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlll.winner.business.model.media.MediaNewChatRecords;
import com.zlll.winner.business.service.common.IRedisService;
import com.zlll.winner.business.service.media.IMediaNewChatRecordsService;
import com.zlll.winner.enums.CommonEnum;
import com.zlll.winner.enums.media.MediaEnum;
import com.zlll.winner.mapper.media.MediaNewChatRecordsMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 节目聊天记录表 服务实现类
 * </p>
 *
 * @author Walert
 * @since 2019-10-04
 */
@Service
@Slf4j
public class MediaNewChatRecordsServiceImpl
        extends ServiceImpl<MediaNewChatRecordsMapper, MediaNewChatRecords>
        implements IMediaNewChatRecordsService {

    @Autowired
    private IRedisService redisService;

    /**
     * 添加聊天记录
     *
     * @param params
     * @return
     */
    @Override
    public Object addChatRecord(JSONObject params) {
        Long bizId = params.getLong("bizId");
        Integer bizType = params.getInteger("bizType");
        Long userId = params.getLong("userId");
        String userImg = params.getString("userImg");
        String userName = params.getString("userName");
        String content = params.getString("content");
        long createTime = System.currentTimeMillis();

        MediaNewChatRecords saveObj = new MediaNewChatRecords();

        saveObj.setBizId(bizId);
        saveObj.setBizType(bizType);
        saveObj.setUserId(userId);
        saveObj.setUserImg(userImg);
        saveObj.setUserName(userName);
        saveObj.setContent(content);
        saveObj.setIssueTime(createTime);
        saveObj.setCreateTime(createTime);
        saveObj.setIsDel(CommonEnum.DelEnum.UNDEL.val());
        int insert = baseMapper.insert(saveObj);
        if(insert > 0 ){
            if (bizType.equals(MediaEnum.TypeEnum.NEWLIST.val())){
                redisService.setRedisByString("living.refresh.version",saveObj.getIssueTime().toString(),60*60);
                redisService.setRedisByString("audio.refresh.version-" + bizId,saveObj.getIssueTime().toString(),60*60);
            }else if(bizType.equals(MediaEnum.TypeEnum.VIDEOLIST.val())){
                redisService.setRedisByString("video.refresh.version-" + bizId,saveObj.getIssueTime().toString(),60*60);
            }
        }
        return saveObj;
    }

    /**
     * 获取三小时内的聊天记录
     *
     * @param params
     * @return
     */
    @Override
    public Object getThreeHourChatRecords(JSONObject params) {
        Integer pageNo = params.getInteger("pageNo");
        Integer pageSize = params.getInteger("pageSize");
        IPage<MediaNewChatRecords> page = new Page<>(pageNo, pageSize);
        long currentTime = System.currentTimeMillis();
        Long lastTime = params.getLong("lastTime");
        if(lastTime == null){
            lastTime = currentTime;
        }
        long threeHoursTime = 3 * 60 * 60 * 1000L;
        long searchTime = currentTime - threeHoursTime;
        return baseMapper.selectPage(page,
                Wrappers.<MediaNewChatRecords>lambdaQuery()
                        .eq(MediaNewChatRecords::getBizType, MediaEnum.TypeEnum.NEWLIST.val())
                        .ge(MediaNewChatRecords::getIssueTime,searchTime)
                        .le(MediaNewChatRecords::getIssueTime,lastTime)
                        .eq(MediaNewChatRecords::getIsDel, CommonEnum.DelEnum.UNDEL.val())
                        .orderByDesc(MediaNewChatRecords::getIssueTime));
    }

    /**
     * 获取节目聊天记录
     *
     * @param params
     * @return
     */
    @Override
    public Object getNewListChatRecords(JSONObject params) {
        Integer pageNo = params.getInteger("pageNo");
        Integer pageSize = params.getInteger("pageSize");
        IPage<MediaNewChatRecords> page = new Page<>(pageNo, pageSize);
        Long bizId = params.getLong("bizId");
        Integer bizType = params.getInteger("bizType");
        Long lastTime = params.getLong("lastTime");
        long currentTime = System.currentTimeMillis();
        if(lastTime == null){
            lastTime = currentTime;
        }
        return baseMapper.selectPage(page,
                Wrappers.<MediaNewChatRecords>lambdaQuery()
                        .eq(MediaNewChatRecords::getBizId,bizId)
                        .eq(MediaNewChatRecords::getBizType,bizType)
                        .eq(MediaNewChatRecords::getIsDel, CommonEnum.DelEnum.UNDEL.val())
                        .le(MediaNewChatRecords::getIssueTime,lastTime)
                        .orderByDesc(MediaNewChatRecords::getIssueTime));
    }

    /**
     * 刷新聊天记录
     * @param params
     * @return
     */
    @Override
    public Object refreshChatRecords(JSONObject params){
        String lastTime = params.getString("lastTime");
        String version = redisService.getRedisByString("living.refresh.version");
        List<MediaNewChatRecords> result = new ArrayList<>();
        if(!StringUtils.isNotBlank(lastTime)){
            lastTime = System.currentTimeMillis()+"";
        }
        if(!lastTime.equals(version)){
            result = baseMapper.selectList(
                    Wrappers.<MediaNewChatRecords>lambdaQuery()
                            .eq(MediaNewChatRecords::getBizType, MediaEnum.TypeEnum.NEWLIST.val())
                            .gt(MediaNewChatRecords::getIssueTime,lastTime)
                            .eq(MediaNewChatRecords::getIsDel, CommonEnum.DelEnum.UNDEL.val())
                            .orderByAsc(MediaNewChatRecords::getIssueTime));
        }
        return result;
    }


    /**
     * 刷新聊天记录
     * @param params
     * @return
     */
    @Override
    public Object refreshAudioChatRecords(JSONObject params){
        String lastTime = params.getString("lastTime");
        Long bizId = params.getLong("bizId");
        if(!StringUtils.isNotBlank(lastTime)){
            lastTime = System.currentTimeMillis()+"";
        }
        String version = redisService.getRedisByString("audio.refresh.version-"+bizId);
        List<MediaNewChatRecords> result = new ArrayList<>();
        if(!lastTime.equals(version)){
            result = baseMapper.selectList(
                    Wrappers.<MediaNewChatRecords>lambdaQuery()
                            .eq(MediaNewChatRecords::getBizId,bizId)
                            .eq(MediaNewChatRecords::getBizType, MediaEnum.TypeEnum.NEWLIST.val())
                            .gt(MediaNewChatRecords::getIssueTime,lastTime)
                            .eq(MediaNewChatRecords::getIsDel, CommonEnum.DelEnum.UNDEL.val())
                            .orderByAsc(MediaNewChatRecords::getIssueTime));
        }
        return result;
    }




    /**
     * 刷新视频聊天记录
     * @param params
     * @return
     */
    @Override
    public Object refreshVideoChatRecords(JSONObject params){
        String lastTime = params.getString("lastTime");
        Long bizId = params.getLong("bizId");
        if(!StringUtils.isNotBlank(lastTime)){
            lastTime = System.currentTimeMillis()+"";
        }
        String version = redisService.getRedisByString("video.refresh.version-" + bizId);
        List<MediaNewChatRecords> result = new ArrayList<>();
        if(!lastTime.equals(version)){
            result = baseMapper.selectList(
                    Wrappers.<MediaNewChatRecords>lambdaQuery()
                            .eq(MediaNewChatRecords::getBizId,bizId)
                            .eq(MediaNewChatRecords::getBizType, MediaEnum.TypeEnum.VIDEOLIST.val())
                            .gt(MediaNewChatRecords::getIssueTime,lastTime)
                            .eq(MediaNewChatRecords::getIsDel, CommonEnum.DelEnum.UNDEL.val())
                            .orderByAsc(MediaNewChatRecords::getIssueTime));
        }
        return result;
    }

}
